package cn.tedu.charging.device.dao.repository.impl;

import cn.tedu.charging.device.dao.repository.AbstractStationCacheRepository;
import cn.tedu.charging.device.dao.repository.StationCacheRepository;
import cn.tedu.charging.device.pojo.po.StationCanalPO;
import cn.tedu.charging.device.pojo.po.StationPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisCommands;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.*;

@Slf4j
@Repository("GeoAndString")
public class StationCacheGeoAndStringRepositoryImpl
        extends AbstractStationCacheRepository
        implements StationCacheRepository {
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 通过Redis String 类型
     * multiSet 批量保存 充电站基本信息
     *
     * @param stationPOs
     */
    @Override
    public void saveAllStation(List<StationPO> stationPOs) {
        saveStationInfoByString(stationPOs);
        saveLocationByGEOAndMemberIsId(stationPOs);
    }

    /**
     * 子类实现 抽象类的方法 返回  redisTemplate
     * @return
     */
    @Override
    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }

    /**
     * 通过Redis String 类型
     * multiSet 批量保存 充电站基本信息
     * @param stationPOs
     */
    private void saveStationInfoByString(List<StationPO> stationPOs) {
        Map<String,StationPO> map = new HashMap<>();
        for (StationPO stationPO : stationPOs) {
            //一条数据就是一个String的k-v
            String key = "station_info_string_"+stationPO.getId();
            map.put(key, stationPO);
        }
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.multiSet(map);
    }

    @Override
    public List<StationPO> search(Double lng, Double lat, Double radius) {
        return super.search(lng, lat, radius);
    }

    @Override
    public void update(StationCanalPO stationCanalPO) {

    }

    @Override
    public void insert(StationCanalPO stationCanalPO) {

    }

    @Override
    public void delete(StationCanalPO stationCanalPO) {

    }

    /**
     * valueOperations.multiGet(stationIds) 批量查
     * @param stationId
     * @return
     */
    @Override
    public StationPO getStationInfoById(String stationId) {
        ValueOperations<String,StationPO> valueOperations = redisTemplate.opsForValue();
        String key = "station_info_string_" + stationId;
        return valueOperations.get(key);
    }


}
